// var gulp = require('gulp'),
//     gulpLoadPlugins = require('gulp-load-plugins'),
//     plugins = gulpLoadPlugins();

var gulp = require('gulp'),
        $ = require('gulp-load-plugins')({rename: {'gulp-rev-append': 'rev'}});

var browserSync = require('browser-sync').create();
var reload      = browserSync.reload;


var AUTOPREFIXER_BROWSERS = ["Android >= 4", "Explorer >= 10", "iOS >= 6"];

// src and  dist, src and build, dev and dist
// dist/build 部署  dist/demo 演示

// 路径变量 // http://www.cnblogs.com/morong/p/4469637.html
var path = {
    // 开发环境
    src: {
        root: './src',
        html: './src/html',
        css: './src/css',
        js: './src/js',
        image: './src/image',
        sass: './src/sass',
        less: './src/less',
        vendor: './src/vendor'
    },

    // 发布环境
    build: {
        root: './dist/build',
        html: './dist/build/html',
        css: './dist/build/css',
        js: './dist/build/js',
        image: './dist/build/image',
        vendor: './dist/build/vendor'
    },

    // 演示
    demo: {
        root: './dist/demo',
        html: './dist/demo/html',
        css: './dist/demo/css',
        js: './dist/demo/js',
        image: './dist/demo/image',
        vendor: './dist/demo/vendor'
    }
};

gulp.task('less', function () {
    gulp.src(['src/less/{app,demo,anim}.less'])
        .pipe($.plumber({errorHandler: $.notify.onError('Error: <%= error.message %>')}))
        .pipe($.sourcemaps.init())
        .pipe($.less())
        .pipe($.autoprefixer({
            browsers: AUTOPREFIXER_BROWSERS, cascade: false
        }))
        .pipe($.sourcemaps.write('./'))
        // .pipe(gulp.dest('src/css'))
        .pipe(gulp.dest('dist/build/css'));
});

gulp.task('concat', function () {
    gulp.src(['src/js/source/miju.js',
        'src/js/source/miju.class.js',
        'src/js/source/**/*.js',
        '!src/js/source/_miju.plugin.tpl.js'])
        .pipe($.concat('miju.js'))
        .pipe(gulp.dest('src/js'));
});

gulp.task('build:concat', function () {
    gulp.src(['src/js/source/miju.js',
            'src/js/source/miju.class.js',
            'src/js/source/**/*.js',
            '!src/js/source/_miju.plugin.tpl.js'])
        .pipe($.concat('miju.js'))
        .pipe(gulp.dest('dist/build/js'));
});


// 这个为何移动不了?
gulp.task('build:js', function () {
    // gulp.src(['src/js/{miju.js, miju.flexible.js}'])
    gulp.src(['src/js/miju.js','src/js/miju.flexible.js}'])
        .pipe(gulp.dest('dist/build/js'));
});

gulp.task('build:copyjs',  function() {
    gulp.src(['src/js/*', '!src/js/source'])
        .pipe(gulp.dest('dist/build/js'))
});

gulp.task('build:uglify', function () {
    gulp.src(['src/js/{miju.js,miju.flexible.js}'])
        .pipe($.uglify())
        .pipe(gulp.dest('dist/build/js'));
});


gulp.task('less:watch', function () {
    gulp.watch('./src/less/**/*.less', ['less']);

    // gulp.watch('src/less/**/*.less', ['less']);
    gulp.watch('src/js/source/**/*.js', ['build:concat']);
});

gulp.task('html:watch', function () {
    gulp.watch(['src/html/**/*.html', 'src/html/*.html', 'src/index.html', 'src/libs/**/*.html'], ['build:html']);

    // gulp.watch('src/less/**/*.less', ['less']);
    // gulp.watch('src/js/source/**/*.js', ['concat']);
});

// http://stackoverflow.com/questions/22391527/gulps-gulp-watch-not-triggered-for-new-or-deleted-files
gulp.task('build:html:watch', function () {
    $.watch(['src/html/**/*.html', 'src/html/*.html', 'src/index.html', 'src/libs/**/*.html'], function() {
        gulp.start( 'build:html' );
    });
});


gulp.task('watch', function () {
    gulp.watch('./src/css/**/*.css').on("change", reload);
    gulp.watch(['./src/html/**/*.html', './src/examples/**/*.html']).on("change", reload);

    gulp.watch('./dist/build/css/**/*.css').on("change", reload);
    gulp.watch('./dist/build/html/**/*.html', ['build:html']).on("change", reload);
    gulp.watch('./dist/build/html/*.html').on("change", reload);
});

// http://stackoverflow.com/questions/22391527/gulps-gulp-watch-not-triggered-for-new-or-deleted-files

// Static server
gulp.task('webserver', function() {
    browserSync.init({
        server: {
            baseDir: "./dist/build"
        },
        port: 8033
    });
});

gulp.task('build:html', function () {
    console.log('xxx');
    gulp.src(['src/html/**/*.html', 'src/index.html', 'src/libs/**/*.html'])
        .pipe($.rev())
        .pipe(gulp.dest('dist/build'));
});

gulp.task('test:html', function () {
    gulp.src(['./src/html/111.html'])
        .pipe($.rev())
        .pipe(gulp.dest('./dist/demo'));
});

// gulp.task('dev', ['less', 'concat', 'watch']);

gulp.task('dev', ['less', 'less:watch', 'webserver', 'watch', 'build:html', 'build:html:watch']);

var dirname = __dirname;

gulp.task('sass:mui', function () {
    gulp.src(['src/sass/mui/mui.scss'])
        .pipe($.plumber({errorHandler: $.notify.onError('Error: <%= error.message %>')}))
        .pipe($.sourcemaps.init())
        .pipe($.sass().on('error', $.sass.logError))
        // .pipe($.autoprefixer({
        //     browsers: AUTOPREFIXER_BROWSERS, cascade: false
        // }))
        .pipe($.sourcemaps.write('./'))
        .pipe(gulp.dest('src/css'));
        // .pipe(gulp.dest('sr/build/css'));
});

gulp.task('sass:test', function () {
    gulp.src(['./src/sass/test/toast.scss'])
        .pipe($.plumber({errorHandler: $.notify.onError('Error: <%= error.message %>')}))
        .pipe($.sourcemaps.init())
        .pipe($.sass().on('error', $.sass.logError))
        // .pipe($.autoprefixer({
        //     browsers: AUTOPREFIXER_BROWSERS, cascade: false
        // }))
        .pipe($.sourcemaps.write('./'))
        .pipe(gulp.dest('src/css'));
    // .pipe(gulp.dest('sr/build/css'));
});